import java.awt.EventQueue;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import org.scilab.modules.javasci.JavasciException;
import org.scilab.modules.javasci.Scilab;
import org.scilab.modules.javasci.JavasciException.InitializationException;
import org.scilab.modules.types.ScilabType;

/*
 * Test001.java
 *
 * Created on __DATE__, __TIME__
 */

/**
 *
 * @author  __USER__
 */
public class Test001 extends javax.swing.JFrame {

	/** Creates new form Test001 */
	private DefaultTableModel t2 = new javax.swing.table.DefaultTableModel(
			new Object[][] { { null, null, null }, { null, null, null },
					{ null, null, null } },
			new String[] { "one", "two", "three" });

	private DefaultTableModel t1 = new javax.swing.table.DefaultTableModel(
			new Object[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } },
			new String[] { "one", "two", "three" });

	public Test001() {
		initComponents();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {
		bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

		jScrollPane1 = new javax.swing.JScrollPane();
		jTable1 = new javax.swing.JTable();
		jScrollPane2 = new javax.swing.JScrollPane();
		jTable2 = new javax.swing.JTable();
		jButton1 = new javax.swing.JButton();
		jTextField1 = new javax.swing.JTextField();
		jComboBox1 = new javax.swing.JComboBox();
		jTextField2 = new javax.swing.JTextField();
		jButton2 = new javax.swing.JButton();
		menuBar = new javax.swing.JMenuBar();
		fileMenu = new javax.swing.JMenu();
		openMenuItem = new javax.swing.JMenuItem();
		saveMenuItem = new javax.swing.JMenuItem();
		saveAsMenuItem = new javax.swing.JMenuItem();
		exitMenuItem = new javax.swing.JMenuItem();
		editMenu = new javax.swing.JMenu();
		cutMenuItem = new javax.swing.JMenuItem();
		copyMenuItem = new javax.swing.JMenuItem();
		pasteMenuItem = new javax.swing.JMenuItem();
		deleteMenuItem = new javax.swing.JMenuItem();
		helpMenu = new javax.swing.JMenu();
		contentsMenuItem = new javax.swing.JMenuItem();
		aboutMenuItem = new javax.swing.JMenuItem();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jTable1.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] { { "one", null, null, null },
						{ "two", null, null, null },
						{ "three", null, null, null } }, new String[] { "one", "two", "three" }));
		jTable1.setColumnSelectionAllowed(true);
		jTable1.setName("a");

		org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings
				.createAutoBinding(
						org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE,
						jTable1, org.jdesktop.beansbinding.ObjectProperty
								.create(), jTable1,
						org.jdesktop.beansbinding.BeanProperty
								.create("elements"));
		bindingGroup.addBinding(binding);
		binding.bind();
		jScrollPane1.setViewportView(jTable1);

		jTable2.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] { { "one", null, null, null },
						{ "two", null, null, null },
						{ "three", null, null, null } }, new String[] { "one", "two", "three" }));
		jScrollPane2.setViewportView(jTable2);

		jButton1.setText("\u8f6c\u7f6e");
		jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				jButton1MouseClicked(evt);
			}
		});
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		jTextField1
				.setText("\u7528b\u77e9\u9635\u8868\u793aa\u77e9\u9635\u7684\u81ea\u8fd0\u7b97\u7ed3\u679c");
		jTextField1.setEnabled(false);

		jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
				"+", "-", "*", "/" }));

		jTextField2.setText("\u586b\u5165\u6570\u5b57");

		jButton2.setText("\u8ba1\u7b97\u7ed3\u679c");
		jButton2.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton2ActionPerformed(evt);
			}
		});

		fileMenu.setText("File");

		openMenuItem.setText("\u5e7b\u706f\u7247\u5c55\u793a");
		openMenuItem.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				openMenuItemActionPerformed(evt);
			}
		});
		fileMenu.add(openMenuItem);

		saveMenuItem.setText("Save");
		fileMenu.add(saveMenuItem);

		saveAsMenuItem.setText("Save As ...");
		fileMenu.add(saveAsMenuItem);

		exitMenuItem.setText("Exit");
		exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				exitMenuItemActionPerformed(evt);
			}
		});
		fileMenu.add(exitMenuItem);

		menuBar.add(fileMenu);

		editMenu.setText("Edit");

		cutMenuItem.setText("Cut");
		editMenu.add(cutMenuItem);

		copyMenuItem.setText("Copy");
		editMenu.add(copyMenuItem);

		pasteMenuItem.setText("Paste");
		editMenu.add(pasteMenuItem);

		deleteMenuItem.setText("Delete");
		editMenu.add(deleteMenuItem);

		menuBar.add(editMenu);

		helpMenu.setText("Help");

		contentsMenuItem.setText("Contents");
		helpMenu.add(contentsMenuItem);

		aboutMenuItem.setText("About");
		helpMenu.add(aboutMenuItem);

		menuBar.add(helpMenu);

		setJMenuBar(menuBar);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addGap(29, 29, 29)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addComponent(
																				jButton1)
																		.addGap(
																				18,
																				18,
																				18)
																		.addComponent(
																				jComboBox1,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				jTextField2,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																		.addComponent(
																				jButton2))
														.addGroup(
																layout
																		.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.TRAILING)
																		.addComponent(
																				jScrollPane1,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				271,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addComponent(
																				jScrollPane2,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				271,
																				javax.swing.GroupLayout.PREFERRED_SIZE))
														.addComponent(
																jTextField1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																223,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(471, Short.MAX_VALUE)));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addComponent(
												jScrollPane1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												94,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(2, 2, 2)
										.addComponent(
												jTextField1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jButton1)
														.addComponent(
																jComboBox1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																jTextField2,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jButton2))
										.addGap(18, 18, 18)
										.addComponent(
												jScrollPane2,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												94,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(124, 124, 124)));

		bindingGroup.bind();
//		addWindowListener(new WindowAdapter() {
//			   public void windowClosing(WindowEvent e) {
//				   int a = MessageBox.Show("真的要退出吗?",12);
//						    if (a == 0) {  
//						     System.exit(0);  //exit
//						    }
//			    }
//		});
		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Huandeng huandeng = new Huandeng();
					huandeng.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
		//zhuanzhi
		if (jTextField2.getText() != null
				&& StringUtils.isNumber(jTextField2.getText())) {
			if (SciUtils.checkdata(this.jTable1)) {
				t2.setDataVector(SciUtils.sciEngin(t1.getDataVector(),
						jComboBox1.getSelectedIndex(), jTextField2.getText()),
						null);
				jTable2.setModel(t2);
				jTable2.setEnabled(true);
				jTable2.setVisible(true);
			}
		} else {
			MessageBox.Show("请输入数字!");
		}
	}

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
		if (SciUtils.checkdata(this.jTable1)) {
			System.out.println(t1.getDataVector().toString());
			zhuanzhi(this.jTable1, t2);
		}

	}

	/*
	 * function zhuanzhi
	 * return boolean
	 * ����������ת��
	 * 
	 * */
	private boolean zhuanzhi(JTable tab1, DefaultTableModel tm) {
		String tablemodel = StringUtils.table2string(tab1);
		tm.setDataVector(sciEngin(StringUtils.format4Table2sci(tablemodel)),
				new String[] { "one", "two", "three" });
		jTable2.setModel(tm);
		jTable2.setTableHeader(jTable1.getTableHeader());
		jTable2.setVisible(true);
		jTable2.setEnabled(true);
		return true;

	}

	private String[][] sciEngin(String tablemodel) {
		//String send = StringUtils.format4Table2sci(tablemodel);
		String[][] recive = null;
		ScilabType scitype = null;
		Scilab sci;
		try {
			sci = new Scilab();

			if (sci.open()) {
				sci.exec(tablemodel);
				if (sci.isExistingVariable(StringUtils.BEGIN)) {
					scitype = sci.get(StringUtils.BEGIN);
					System.out.println("scitype:" + scitype.toString());
					recive = StringUtils.format4sci2table(scitype.toString(),
							scitype.getHeight(), scitype.getWidth());
				}
				sci.close();
			}

		} catch (InitializationException e) {
			e.printStackTrace();
		} catch (JavasciException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return recive;

	}

	private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
	}

	private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
		System.exit(0);
		
	}//GEN-LAST:event_exitMenuItemActionPerformed

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new Test001().setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JMenuItem aboutMenuItem;
	private javax.swing.JMenuItem contentsMenuItem;
	private javax.swing.JMenuItem copyMenuItem;
	private javax.swing.JMenuItem cutMenuItem;
	private javax.swing.JMenuItem deleteMenuItem;
	private javax.swing.JMenu editMenu;
	private javax.swing.JMenuItem exitMenuItem;
	private javax.swing.JMenu fileMenu;
	private javax.swing.JMenu helpMenu;
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JComboBox jComboBox1;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JTable jTable1;
	private javax.swing.JTable jTable2;
	private javax.swing.JTextField jTextField1;
	private javax.swing.JTextField jTextField2;
	private javax.swing.JMenuBar menuBar;
	private javax.swing.JMenuItem openMenuItem;
	private javax.swing.JMenuItem pasteMenuItem;
	private javax.swing.JMenuItem saveAsMenuItem;
	private javax.swing.JMenuItem saveMenuItem;
	private org.jdesktop.beansbinding.BindingGroup bindingGroup;
	// End of variables declaration//GEN-END:variables

}